﻿using Nop.Admin.Models.WindAlert;
using Nop.Admin.WindAlert;
using Nop.Core;
using Nop.Core.Domain.Common;
using Nop.Services.ExportImport;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Security;
using Nop.Services.WindAlert;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;
using System;
using System.Linq;
using System.Web.Mvc;


namespace Nop.Admin.Controllers
{
    public partial class WMobileDeviceController : BaseAdminController
    {
        #region Fields
        private readonly ICustomerActivityService _customerActivityService;
        private readonly ILocalizationService _localizationService;
        private readonly IExportManager _exportManager;
        private readonly IWorkContext _workContext;
        private readonly IPermissionService _permissionService;
        private readonly AdminAreaSettings _adminAreaSettings;
        private readonly IMobileDeviceService _mobileDeviceService;
        #endregion

        #region Constructors

        public WMobileDeviceController(
            ICustomerActivityService customerActivityService,
            ILocalizationService localizationService,
            IExportManager exportManager, IWorkContext workContext,
            IPermissionService permissionService,
            AdminAreaSettings adminAreaSettings,
            IMobileDeviceService mobileDeviceService)
        {
            this._customerActivityService = customerActivityService;
            this._localizationService = localizationService;
            this._exportManager = exportManager;
            this._workContext = workContext;
            this._permissionService = permissionService;
            this._adminAreaSettings = adminAreaSettings;
            this._mobileDeviceService = mobileDeviceService;
        }

        #endregion

        #region Methods
        #region Create / Edit / Delete / List
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            return View();
        }

        [HttpPost]
        public ActionResult List(DataSourceRequest command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();
            var mobileDevices = _mobileDeviceService.GetAllMobileDevices(command.Page - 1, command.PageSize);
            var gridModel = new DataSourceResult
            {
                Data = mobileDevices.Select(x => x.ToModel()),
                Total = mobileDevices.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Create()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            var model = new MobileDeviceModel();
            //default values
            model.Active = true;
            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Create(MobileDeviceModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            if (ModelState.IsValid)
            {
                var mobileDevice = model.ToEntity();
                mobileDevice.CreatedOnUtc = DateTime.UtcNow;
                mobileDevice.UpdatedOnUtc = DateTime.UtcNow;
                _mobileDeviceService.InsertMobileDevice(mobileDevice);
                
                //activity log
                _customerActivityService.InsertActivity("AddNewMobileDevice", _localizationService.GetResource("ActivityLog.AddNewMobileDevice"), mobileDevice.DeviceId);

                SuccessNotification(_localizationService.GetResource("Admin.WindAlert.MobileDevice.Added"));
                return continueEditing ? RedirectToAction("Edit", new { id = mobileDevice.Id }) : RedirectToAction("List");
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            var mobileDevice = _mobileDeviceService.GetMobileDeviceById(id);
            if (mobileDevice == null)
                return RedirectToAction("List");

            var model = mobileDevice.ToModel();
            model.CreatedOn = mobileDevice.CreatedOnUtc;
            model.UpdatedOn = mobileDevice.UpdatedOnUtc;
            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Edit(MobileDeviceModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            var mobileDevice = _mobileDeviceService.GetMobileDeviceById(model.Id);
            if (mobileDevice == null)	
                return RedirectToAction("List");

            if (ModelState.IsValid)
            {
                mobileDevice = model.ToEntity(mobileDevice);
                mobileDevice.UpdatedOnUtc = DateTime.UtcNow;
                _mobileDeviceService.UpdateMobileDevice(mobileDevice);

                //activity log
                _customerActivityService.InsertActivity("EditMobileDevice", _localizationService.GetResource("ActivityLog.EditMobileDevice"), mobileDevice.DeviceId);

                SuccessNotification(_localizationService.GetResource("Admin.WindAlert.MobileDevice.Updated"));
                if (continueEditing)
                {
                    //selected tab
                    SaveSelectedTabIndex();

                    return RedirectToAction("Edit", new { id = mobileDevice.Id });
                }
                else
                {
                    return RedirectToAction("List");
                }
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
                return AccessDeniedView();

            var mobileDevice = _mobileDeviceService.GetMobileDeviceById(id);
            if (mobileDevice == null)	
                return RedirectToAction("List");
            _mobileDeviceService.DeleteMobileDevice(mobileDevice);

            //activity log
            _customerActivityService.InsertActivity("DeleteMobileDevice", _localizationService.GetResource("ActivityLog.DeleteMobileDevice"), mobileDevice.DeviceId);

            SuccessNotification(_localizationService.GetResource("Admin.WindAlert.MobileDevice.Deleted"));
            return RedirectToAction("List");
        }
        #endregion


        #endregion

    }
}
